//
//  ChatBoxFaceView.m
//  ChangChat
//
//  Created by Cocoa on 2019/8/24.
//  Copyright © 2019 yibai. All rights reserved.
//

#import "ChatBoxFaceView.h"
#import "ChatFaceContentView.h"

@interface ChatBoxFaceView ()<UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *scrollView;
@property (nonatomic, assign) NSInteger currentIndex;
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) NSMutableArray *faceItemArray;
@property (nonatomic, strong)  NSMutableArray *facePageViewArray;

@end

@implementation ChatBoxFaceView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
        [self loadDatasource];
    }
    return self;
}

- (void)setupUI {
    
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectZero];
    [self.scrollView setScrollsToTop:NO];
    [self.scrollView setShowsVerticalScrollIndicator:NO];
    [self.scrollView setShowsHorizontalScrollIndicator:NO];
    self.scrollView.delegate = self;
    self.scrollView.pagingEnabled = YES;
    
    self.pageControl = [[UIPageControl alloc] initWithFrame:CGRectZero];
    self.pageControl.currentPageIndicatorTintColor = [UIColor grayColor];
    self.pageControl.pageIndicatorTintColor = [UIColor lightGrayColor];
    [self.pageControl addTarget:self action:@selector(pageControlClicked:) forControlEvents:UIControlEventValueChanged];
    
    
    [self addSubview:self.scrollView];
    [self addSubview:self.pageControl];
    self.facePageViewArray = [NSMutableArray arrayWithCapacity:3];
    for (int i = 0; i < 3; i++) {
        ChatFaceContentView *faceContentView = [[ChatFaceContentView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, self.height-KSafeAreaBottom)];
        [self.facePageViewArray addObject:faceContentView];
        [self.scrollView addSubview:faceContentView];
    }
    
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.top.mas_offset(0);
        make.bottom.equalTo(self.pageControl.mas_top).offset(-3);
        make.height.mas_equalTo(200);
    }];
    
    [self.pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.bottom.mas_offset(-10);
        make.height.mas_equalTo(8);
    }];
}
- (void)loadDatasource {
    NSArray *array = [NSArray arrayWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"normal_face" ofType:@"plist"]];
    
    self.faceItemArray = [ChatFaceItem mj_objectArrayWithKeyValuesArray:array];
    [self reloadScrollView];
}
#pragma mark - scrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    int page = scrollView.contentOffset.x / self.scrollView.width;
    if (page > self.currentIndex && (page * ScreenWidth - scrollView.contentOffset.x) < ScreenWidth*0.2) {
        [self showFacePageAtIndex:page];
    } else if (page < self.currentIndex && (scrollView.contentOffset.x - page * ScreenWidth) < ScreenWidth * 0.2) {
        [self showFacePageAtIndex:page];
    }
}
#pragma mark - actions
- (void)pageControlClicked:(UIPageControl *)sender {
    [self showFacePageAtIndex:sender.currentPage];
    [self.scrollView scrollRectToVisible:CGRectMake(sender.currentPage*ScreenWidth, 0, ScreenWidth, self.scrollView.height) animated:YES];
}
- (void)showFacePageAtIndex:(NSInteger)index {
    if (self.currentIndex == index) {
        return;
    }
    
    NSLog(@"\n%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
    
    [self.pageControl setCurrentPage:index];
    int count = 20;
    if (self.currentIndex == -1) {
        @weakify(self);
        ChatFaceContentView *contentView1 = [self.facePageViewArray objectAtIndex:0];
        [contentView1 showFaceGroup:self.faceItemArray fromIndex:0 count:0];
        contentView1.origin = CGPointMake(-ScreenWidth, 0);
        [contentView1 setClickFaceItemViewAction:^(UIButton * _Nonnull sender) {
            @strongify(self);
            [self didSelectFace:sender];
        }];
        
        ChatFaceContentView *contentView2 = [self.facePageViewArray objectAtIndex:1];
        [contentView2 showFaceGroup:self.faceItemArray fromIndex:0 count:count];
        contentView2.origin = CGPointMake(0, 0);
        [contentView2 setClickFaceItemViewAction:^(UIButton * _Nonnull sender) {
            @strongify(self);
            [self didSelectFace:sender];
        }];
        
        ChatFaceContentView *contentView3 = [self.facePageViewArray objectAtIndex:2];
        [contentView3 showFaceGroup:self.faceItemArray fromIndex:count count:count];
        contentView3.origin = CGPointMake(ScreenWidth, 0);
        [contentView3 setClickFaceItemViewAction:^(UIButton * _Nonnull sender) {
            @strongify(self);
            [self didSelectFace:sender];
        }];
    } else {
        if (self.currentIndex < index) {
            ChatFaceContentView *contentView1 = [self.facePageViewArray objectAtIndex:0];
            [contentView1 showFaceGroup:self.faceItemArray fromIndex:(index + 1)*count count:count];
            contentView1.origin = CGPointMake((index + 1)*ScreenWidth, 0);
            [self.facePageViewArray removeObjectAtIndex:0];
            [self.facePageViewArray addObject:contentView1];
        } else {
            ChatFaceContentView *contentView3 = [self.facePageViewArray objectAtIndex:2];
            [contentView3 showFaceGroup:self.faceItemArray fromIndex:(index - 1)*count count:count];
            contentView3.origin = CGPointMake((index - 1)*ScreenWidth, 0);
            [self.facePageViewArray removeLastObject];
            [self.facePageViewArray insertObject:contentView3 atIndex:0];
        }
    }
    self.currentIndex = index;
}
- (void)didSelectFace:(UIButton *)sender {
    if (sender.tag == -1) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBoxFaceViewDeleteBtnAction)] == YES) {
            [self.delegate chatBoxFaceViewDeleteBtnAction];
        }
    } else {
        ChatFaceItem *face = self.faceItemArray[sender.tag];
        if (self.delegate && [self.delegate respondsToSelector:@selector(chatBoxFaceViewDidSelectFace:)] == YES) {
            [self.delegate chatBoxFaceViewDidSelectFace:face];
        }
    }
}

- (void)reloadScrollView {
    
    int page = ceil(self.faceItemArray.count / 20.0);
    self.pageControl.numberOfPages = page;
    self.scrollView.contentSize = CGSizeMake(ScreenWidth * page, self.scrollView.height);
    [self.scrollView scrollRectToVisible:CGRectMake(0, 0, ScreenWidth, self.scrollView.height) animated:NO];
    
    self.currentIndex = -1;
    [self showFacePageAtIndex:0];
}

@end
